//导入父类
let HELP_AXIS_ARR = []
let HELP_SUM = 0
let HELP_SERIES_OBJ = {}


import defaultConfig from './default-option'
export default class OptionBar extends defaultConfig {
  constructor({
    rows = [],
    columns = [],
    header = columns[0]
  }, settings = {}, extend = {}) {
    super()
    const keyArr = columns.filter(item => item !== header)
    const axisData = rows.map(item => item[header])
    this.option = this.defaultConfig
    axisData.push('总计')
    this.option.xAxis.data = axisData
    this.option.series = keyArr.map(item => {
      let data = {
        name: item,
        type: 'bar',
        stack: '总量',
        data: rows.map((list, index) => {
          HELP_AXIS_ARR[index] = HELP_AXIS_ARR[index] ? HELP_AXIS_ARR[index] + parseFloat(HELP_SUM) : HELP_SUM
          HELP_SUM += parseFloat(list[item])
          return list[item]
        })
      }
      data.data.push(HELP_SUM + '')
      HELP_SUM = 0
      return data
    })
    this.mySettings(this.option, settings, keyArr)
    this.option = this.setNewStyle(extend, this.option)
  }

  mySettings = (option, settings, keyArr) => {
    this.createNewSeriesObj(option)
    this.setDefaultConfig(option, keyArr)
    if (settings.totalName) {
      const { totalName } = settings
      this.setTotalName(option, totalName)
    }
    if (settings.labelMap) {
      const { labelMap } = settings
      this.setLabelMap(option, labelMap)
    }
    if (settings.sumLocation) {
      const { sumLocation } = settings
      this.setSumLocation(option, sumLocation)
    }
  }
  createNewSeriesObj = (option) => {
    HELP_AXIS_ARR.push(0)
    HELP_SERIES_OBJ = {
      name: '总计',
      type: 'bar',
      stack: '总量',
      itemStyle: {
        barBorderColor: 'rgba(0,0,0,0)',
        color: 'rgba(0,0,0,0)'
      },
      emphasis: {
        itemStyle: {
          barBorderColor: 'rgba(0,0,0,0)',
          color: 'rgba(0,0,0,0)'
        }
      },
      data: HELP_AXIS_ARR
    }
    option.series.unshift(HELP_SERIES_OBJ)
    option.series.forEach(item => {
      item.data
    })
  }

  setDefaultConfig = (option, keyArr) => {
    const myTooltip = {
      formatter: function (params) {
        var tar = params[1]
        return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value
      }
    }
    const myLabel = {
      label: this.label
    }
    const myLegend = {
      data: keyArr
    }
    option.tooltip = this.setNewStyle(myTooltip, option.tooltip)
    option.legend = this.setNewStyle(myLegend, option.legend)
    option.series.forEach(item => {
      item = this.setNewStyle(myLabel, item)
    })
  }
  setTotalName = (option, totalName) => {
    let myData = option.xAxis.data
    myData.forEach((item, index) => {
      myData[index] = item == '总计' ? totalName : item
    })
  }
  setLabelMap = (option, labelMap) => {
    option.legend.formatter = (name) => {
      if (labelMap[name]) return labelMap[name]
      return name
    }

    option.tooltip.formatter = params => {
      params.shift()
      let str = ''
      params.forEach((item, index) => {
        if (index === 0) {
          str += item.name + '<br/>' + labelMap[item.seriesName] + ' : ' + item.value
        } else {
          str += labelMap[item.seriesName] ?
            '<br/>' + labelMap[item.seriesName] + ' : ' + item.value :
            '<br/>' + item.seriesName + ' : ' + item.value
        }
      })
      return str
    }
  }
  setSumLocation = (option, sumLocation) => {
    let axisData = option.xAxis.data
    axisData.unshift(axisData.pop())

    let sumArr = option.series.map((item, index) => {
      let arr = []
      if (index !== 0) {
        let data = item.data
        if (sumLocation == 'left') {
          let sum = data.pop()
          arr[0] ? '' : arr.push(sum)
          data.forEach((item, index) => {
            arr[index + 1] = arr[index] - item
          })
          data.unshift(sum)
        }
      }
      arr.shift()
      return arr
    })
    sumArr.shift()
    let finalArr = []
    sumArr.forEach(item => {
      item.forEach((list, index) => {
        finalArr[index] = finalArr[index] ? finalArr[index] + parseFloat(list) : parseFloat(list)
      })
    })
    finalArr.unshift(0)
    option.series[0].data = finalArr
  }
}